<style type="text/css">
#content-left {
	width: 640px;
	float: left;
	box-shadow: 0px 0px 0px 1px #E8E9EB;
	box-sizing: border-box;
	min-height: 400px;
}

#content-right {
	width: 320px;
	float: left;
}

#lesson-detail {
	border-bottom: 1px solid #E8E9EB;
	padding: 20px;
}

#chapters {
	margin-top: 120px;
}

#chapter-block {
	padding: 20px;
}

.edit {
	color: #BBB;
	font-size: 11px;
	cursor: pointer;
}

.chapter-list {
	background: whiteSmoke;
	border-left: 1px solid #E8E9EB;
	border-bottom: 1px solid #E8E9EB;
	line-height: 40px;
	text-indent: 20px;
	cursor: pointer;
}
.chapter-list:first-child {
	border-top: 1px solid #E8E9EB;
}

.chapter-list.select {
	background: white;
	border-left: none;
}

.del-lesson {
	float: right;
	color: orange;
}
</style>
<div id="lesson-block">
	<div id="content-left">
		<div id="lesson-detail">
			<div><strong><?php echo $lesson->lesson_name;?></strong> <a class="edit">แก้ไข</a></div>
			<div>
				<p><?php echo $lesson->lesson_detail;?> <a class="edit">แก้ไข</a></p>
			</div>
			<div>
				<a class="edit various fancybox.ajax" href="<?php echo $this->createUrl('chapter/create',array('id'=>$lesson->lesson_id));?>">+เพิ่มบทย่อย</a>
				<a class="edit">+เพิ่มรูป</a>
				<a class="edit">+เพิ่มวีดีโอ</a>
				<a class="edit">+แนบไฟล์</a>
				<a href="<?php echo $this->createUrl('lesson/delete/'.$lesson->lesson_id);?>" class="del-lesson">ลบบทเรียนนี้</a>
			</div>
		</div>
		<div id="chapters-block">
			
		</div>
	</div>
	<div id="content-right">
		<ul id="chapters">
		<?php foreach($lesson->chapters as $key => $chapter){ ?>
			<li class="chapter-list"><a class="chapter-name" href="<?php echo $this->createUrl('chapter/'.$chapter->chapter_id);?>"><?php echo $chapter->chapter_name;?></a> <a class="edit">แก้ไข</a> <a class="edit">ลบ</a></li>
		<?php }?>
		</ul>
	</div>
	<div class="clear"></div>
</div>

<script type="text/javascript">
$(function(){
	$('.chapter-list').click(function(event){
		event.preventDefault();
		$(this).addClass('select').siblings().removeClass('select');
		var url = $(this).find('a.chapter-name').attr('href');
		$('#chapters-block').load(url,function(){
			// statement
		});
	});
	$('.chapter-list:first').click();

	$(".various").fancybox({
		maxWidth	: 800,
		maxHeight	: 600,
		fitToView	: false,
		width		: '70%',
		height		: '70%',
		autoSize	: true,
		closeClick	: false,
		openEffect	: 'fade',
		closeEffect	: 'fade'
	});

	$('.del-lesson').click(function(event){
		event.preventDefault();
		if(!confirm('คุณต้องการลบบทเรียนนี้จริงหรือไม่')){ return false; }
		var url = $(this).attr('href');
		$.ajax(url,{
			'dataType': 'JSON',
			'success' : function(data){
				if(data.success){
					alert('ลบเรียบร้อยแล้ว');
					setTimeout(function(){ window.location.href='<?php echo $this->createUrl('site/home');?>'; },1000);
				}
				else{
					console.log(data.errors);
				}
			}
		});
	});
});
</script>